#include <stdio.h>
#include <stdlib.h>
void print(int a[], int n) {
  int i;
  for (i = 0; i < n; i++)
    printf("%d ", a[i]);
  printf("\n");
}
int inc(const void *a, const void *b) {
  int *ia = (int*)a;
  int *ib = (int*)b;
  return  *ia -  *ib;
}
int dec(const void *a, const void *b) {
  int *ia = (int*)a;
  int *ib = (int*)b;
  return  *ib -  *ia;
}
int hash(int n) {
  return n < 10 ? n : hash(n / 10) + (n % 10);
}
int cmp(const void *a, const void *b) {
  int *ia = (int*)a;
  int *ib = (int*)b;
  int flag = hash(*ia) - hash(*ib);
  if (flag)
    return flag;
  return  *ia -  *ib;
}
int main() {
  int a[] =  {
    19, 7, 8, 5, 0, 1, 4, 61, 21111, 11, 123, 3, 82, 28, 22222
  };
  int n = sizeof(a) / sizeof(a[0]);
  print(a, n);
  //  qsort(a,n,4,inc);
  //   qsort(a,n,4,dec);
  qsort(a + 1, n - 1, sizeof(a[0]), cmp);
  print(a, n);
  return 0;
}
